﻿Imports OMANE.Common.UI
Imports PTIC_Util
Imports System.Collections.ObjectModel

Namespace ViewModels
    Public Class ViewModelPreviewBase
        Inherits ViewModelBase

        Sub New()

        End Sub
        Sub New(ByVal data As DataTable)
            Me._Data = data
            For Each c In _Data.Columns
                _allcolumns.Add(c)
            Next
        End Sub


        Private _Data As DataTable
        Public Property Data As DataTable
            Get
                Return _Data
            End Get
            Set(ByVal value As DataTable)
                OnPropertyChanging("Data")
                Dim args As New PropertyChangeEventArgs(Of DataTable)(_Data, value)
                OnDataChange(args)
                _Data = args.NewValue
                OnPropertyChanged("Data")
            End Set
        End Property
        Protected Overridable Sub OnDataChange(ByVal args As PropertyChangeEventArgs(Of DataTable))
            RaiseEvent DataChange(Me, args)
        End Sub
        Public Event DataChange As PropertyChangeEventHandler(Of DataTable)



        Private _pivotfilter As New ObservableCollection(Of DataColumn)
        Private _pivotaggregate As New ObservableCollection(Of DataColumn)
        Private _pivotrow As New ObservableCollection(Of DataColumn)
        Private _pivotcolumn As New ObservableCollection(Of DataColumn)
        Private _allcolumns As New ObservableCollection(Of DataColumn)

        Public ReadOnly Property PivotFilter As ObservableCollection(Of DataColumn)
            Get
                Return _pivotfilter
            End Get
        End Property
        Public ReadOnly Property PivotAggregate As ObservableCollection(Of DataColumn)
            Get
                Return _pivotaggregate
            End Get
        End Property
        Public ReadOnly Property PivotColumn As ObservableCollection(Of DataColumn)
            Get
                Return _pivotcolumn
            End Get
        End Property
        Public ReadOnly Property PivotRow As ObservableCollection(Of DataColumn)
            Get
                Return _pivotrow
            End Get
        End Property
        Public ReadOnly Property AllColumns As ObservableCollection(Of DataColumn)
            Get
                Return _allcolumns
            End Get
        End Property

        Private distinctsCache As New Hashtable
        Public Sub Recalculate()
            Dim qr = From c In _pivotrow Select c.ColumnName Distinct.ToArray()
            Dim qc = From c In _pivotcolumn Select c.ColumnName Distinct.ToArray()

            Dim all = New List(Of String)
            all.AddRange(qr)
            all.AddRange(qc)

            Dim qall = From c In all Select c Distinct.ToArray()

            'Dim rt = Data.DefaultView.ToTable(True, qr.ToArray())
            'Dim ct = Data.DefaultView.ToTable(True, qc.ToArray())

            'For Each c In _pivotaggregate
            '    Dim o = Data.AsDataView().AsQueryable()
            'Next
            If qall.Length > 0 Then
                _PivotTable = Data.DefaultView.ToTable(True, qall)

                Dim qallcolsource = (From c In qall Select Data.Columns(c)).ToArray()
                Dim qallcolpivot = (From c In qall Select PivotTable.Columns(c)).ToArray()

                _PivotTable.TableName = "Table_" & Guid.NewGuid().ToString("N")
                Data.DataSet.Tables.Add(_PivotTable)
                Dim rel As New DataRelation(_PivotTable.TableName & "_Relation", qallcolpivot, qallcolsource)
                Data.DataSet.Relations.Add(rel)

                For Each c In _pivotaggregate
                    If c.DataType.IsValueType Then
                        _PivotTable.Columns.Add(c.ColumnName, c.DataType, "Avg(Child." & c.ColumnName & ")")
                    End If
                Next

              
                OnPropertyChanged("PivotTable")
            End If
        End Sub

        Public Function TableToMatrix(ByVal table As DataTable, ByVal rowfield As String, ByVal colfield As String, ByVal valuefield As String)
            Dim v = table.DefaultView.ToTable(True, rowfield, colfield)
        End Function
        

        Private _PivotTable As DataTable
        Public Property PivotTable As DataTable
            Get
                Return _PivotTable
            End Get
            Set(ByVal value As DataTable)
                OnPropertyChanging("PivotTable")
                _PivotTable = value
                OnPropertyChanged("PivotTable")
            End Set
        End Property



        Private Structure MemoryData

        End Structure
        Private Structure QueryData

        End Structure
    End Class

    Public Class MatrixAccessor

        Private workingTable As DataTable
        Private workingView As DataView
        Private rowcount As Integer
        Private colcount As Integer
        Private rowname As String
        Private colname As String
        Private fieldname As String


        Default Public Property Item(ByVal r As Object, ByVal c As Object) As Object
            Get
                Dim result = workingView.FindRows(New Object() {r, c})
                Return If(result Is Nothing, Nothing, _
                          If(result.Length = 0, Nothing, _
                             result(0)(fieldname)))
            End Get
            Set(ByVal value As Object)
                Dim result = workingView.FindRows(New Object() {r, c})
                If result IsNot Nothing Then
                    If result.Length > 0 Then
                        result(0)(fieldname) = value
                    End If
                End If
            End Set
        End Property
        Default Public Property Item(ByVal r As Integer, ByVal c As Integer) As Object
            Get
                Return workingTable.Rows(r * colcount + c)(fieldname)
            End Get
            Set(ByVal value As Object)
                workingTable.Rows(r * colcount + c)(fieldname) = value
            End Set
        End Property
    End Class
End Namespace